<?php
class login extends base{

	public function init(){
		parent::init();

		$this->tit = '登录';
		$this->modelObj = D('company_admin');
	}

	public function index(){
		if($this->fmdo=='login'){

			$username = arg('username');
			$password = arg('password');

			if (empty($username)) {
				$this->renderError('用户名不能为空');
			}
	        $admin = $this->modelObj->check_login($username,$password);
	        if($admin){
	        	$_SESSION['company_user']['id'] = $admin['admin']['id'];
	            $this->modelObj->up_time($admin['id']);

	            //自动登录
            	if(arg('remember')){
					$this->set_cookie('m_user',$username);
					$this->set_cookie('m_pass',$password);
					$this->set_cookie('remember','1');
				}else{
					$this->set_cookie('m_pass','');
					$this->set_cookie('remember','');
				}
				
	        	$this->renderSuccess('登录成功',"/{$this->module}/main/index");

	        }else{
	            $this->renderError('登录失败');
	        }

		}
	}

	// 退出
	public function logout(){
		// 这里是PHP.net关于删除SESSION的方法
		$_SESSION = array();
		if (isset($_COOKIE[session_name()])) {setcookie(session_name(), '', time()-42000, '/');}
		session_destroy();
		$this->redirect("/login/index");
	}

	//验证码
	public function verify(){
		$config = array(
			'fontSize'   => 18, 
			'length'     => 4,
			'useNoise'   => false,
		);
		$verify = new Verify($config);
		$verify->fontttf = '4.ttf'; 
		$verify->codeSet = '0123456789';
		$verify->entry();
	}

}